package com.atguigu.yygh.hosp.hosp.testmongo;


import com.atguigu.yygh.common.result.R;
import com.mongodb.client.result.DeleteResult;
import com.mongodb.client.result.UpdateResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import java.io.OutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

@RestController
@RequestMapping("/mongo1")
public class TestMongo1 {

    @Autowired
    private MongoTemplate mongoTemplate;


    //添加
    @GetMapping("create")
    public void createUser(){
        User user = new User();
        user.setAge(20);
        user.setName("test");
        user.setEmail("4932200@qq.com");
        User insert = mongoTemplate.insert(user);
        System.out.println("insert = " + insert);
    }

    //查询所有
    @GetMapping("fingAll")
    public void findAll(){
        List<User> all = mongoTemplate.findAll(User.class);
        System.out.println("all = " + all);
    }

    @GetMapping("findById")
    public void findById(){
        User byId = mongoTemplate.findById("61878ea964bd4c034e94a765", User.class);
        System.out.println("byId = " + byId);
    }

    //条件查询
    @GetMapping("findUser")
    public void findUser(){
        Query query = new Query(Criteria.where("name").is("test1").and("age").is(20));

        List<User> users = mongoTemplate.find(query, User.class);
        System.out.println("users = " + users);

    }

    //模糊查询
    @GetMapping("findLike")
    public void findLike(){
        String name = "est";
        String regex = String.format("%s%s%s", "^.*", name, ".*$");
        Pattern compile = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
        Query name1 = new Query(Criteria.where("name").regex(compile));
        List<User> users = mongoTemplate.find(name1, User.class);
        System.out.println("users = " + users);
    }

    //分页查询
    @GetMapping("findPage")
    public void findUserPage(){
        String name = "est";
        int pageNo = 1;
        int pageSize = 10;

        Query query = new Query();
        String regex = String.format("%s%s%s", "^.*", name, ".*$");
        Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
        query.addCriteria(Criteria.where("name").regex(pattern));
        long count = mongoTemplate.count(query, User.class);
        List<User> users = mongoTemplate.find(query.skip((pageNo - 1) * pageSize).limit(pageSize), User.class);
        Map<String, Object> pageMap = new HashMap<>();
        pageMap.put("list", users);
        pageMap.put("totalCount",count);
        System.out.println(pageMap);

    }


    //修改
    @GetMapping("update")
    public void update(){

        User user = mongoTemplate.findById("61878ea964bd4c034e94a765", User.class);
        user.setName("test_1");
        user.setAge(25);
        user.setEmail("493220990@qq.com");
        Query query = new Query(Criteria.where("_id").is(user.getId()));
        Update update = new Update();
        update.set("name", user.getName());
        update.set("age", user.getAge());
        update.set("email", user.getEmail());
        UpdateResult upsert = mongoTemplate.upsert(query, update, User.class);
        long modifiedCount = upsert.getModifiedCount();
        System.out.println("modifiedCount = " + modifiedCount);


    }
    //删除操作
    @GetMapping("delete")
    public void delete() {
        Query query =
                new Query(Criteria.where("_id").is("61878ea964bd4c034e94a765"));
        DeleteResult result = mongoTemplate.remove(query, User.class);
        long count = result.getDeletedCount();
        System.out.println(count);
    }


}

























